package com.openvid.webrtc.openvid.pusher;

import android.content.Context;
import android.util.Log;

import org.webrtc.CapturerObserver;
import org.webrtc.NV12Buffer;
import org.webrtc.NV21Buffer;
import org.webrtc.SurfaceTextureHelper;
import org.webrtc.VideoCapturer;
import org.webrtc.VideoFrame;

import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class FileVideoCapturer implements VideoCapturer {


    private CapturerObserver mCapturerObs;

    @Override
    public void initialize(SurfaceTextureHelper surfaceTextureHelper, Context context, CapturerObserver capturerObserver) {
        mCapturerObs = capturerObserver;
    }

//    private long mStartTime;
//    private SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");

    @Override
    public void startCapture(int width, int height, int fps) {
        mCapturerObs.onCapturerStarted(true);
//        mStartTime=System.nanoTime();
    }


//    private Map<String,Integer> AA=new HashMap<>();
    public void addData(ByteBuffer data, int width, int height, long frameTime) {
        NV12Buffer buffer = new NV12Buffer(width, height,width,height,data, null);
        long f=frameTime*1000;
//        String s=sdf.format(f/1000/1000);
//        Integer a=AA.get(s);
//        if (a==null){
//            a=0;
//        }
//        a++;
//        AA.put(s,a);
//        Log.i("123123","frameTime:"+frameTime+"---sdf:"+s+"---secondFrame:"+a);
        VideoFrame videoFrame = new VideoFrame(buffer, 0, f);
        mCapturerObs.onFrameCaptured(videoFrame);
    }


    @Override
    public void stopCapture() throws InterruptedException {
    }

    @Override
    public void changeCaptureFormat(int w, int h, int fps) {

    }

    @Override
    public void dispose() {
        try {
            stopCapture();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isScreencast() {
        return false;
    }
}
